package com.component;

import com.util.GetImage;
import com.util.ScreenUtils;
import com.util.SelectUsers;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FarmerInterface {
    JFrame jf = new JFrame();

    private int FarmerID;
    final int WIDTH = 850;
    final int HEIGHT = 600;

    //组装视图
    public void init(String fUserName) throws Exception {
        jf.setTitle("扶贫农产品销售系统："+fUserName+"，欢迎您");
        this.FarmerID = new SelectUsers().getUserID(fUserName);
        //给窗口设置属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        //jf.setResizable(false);
        jf.setIconImage(new GetImage().getImage("logo.png"));

        //设置菜单栏
        JMenuBar jmb = new JMenuBar();
        JMenu jMenu = new JMenu("设置");
        JMenuItem m1 = new JMenuItem("切换账号");
        JMenuItem m2 = new JMenuItem("退出程序");
        m1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new FarmingSystemGUI();
                    jf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        m2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        jMenu.add(m1);
        jMenu.add(m2);
        jmb.add(jMenu);

        jf.setJMenuBar(jmb);

        //设置分割面板
        JSplitPane sp = new JSplitPane();

        //支持连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(150);
        sp.setDividerSize(7);

        //设置左侧内容
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("农户的系统管理");
        DefaultMutableTreeNode productManage = new DefaultMutableTreeNode("农产品管理");
        DefaultMutableTreeNode orderManage = new DefaultMutableTreeNode("订单状态更新");
        DefaultMutableTreeNode statisticManage = new DefaultMutableTreeNode("统计农产品的销售信息");

        root.add(productManage);
        root.add(orderManage);
        root.add(statisticManage);

        Color color = new Color(203,220,217);
        JTree tree = new JTree(root);
        MyRenderer myRenderer = new MyRenderer();
        myRenderer.setBackgroundNonSelectionColor(color);
        myRenderer.setBackgroundSelectionColor(new Color(140,140,140));
        tree.setCellRenderer(myRenderer);

        tree.setBackground(color);
        //设置当前tree默认选中农产品管理
        tree.setSelectionRow(1);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            //当条目选中变化后，这个方法会执行
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                //得到当前选中的结点对象
                Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();

                if (productManage.equals(lastPathComponent)){
                    //sp.setRightComponent(new JLabel("这里进行农产品管理..."));
                    sp.setRightComponent(new ProductManageComponent(jf,FarmerID));
                    sp.setDividerLocation(150);
                }else if (orderManage.equals(lastPathComponent)){
                    //sp.setRightComponent(new JLabel("这里进行订单进行状态的更新..."));
                    sp.setRightComponent(new OrderManageComponent(jf,FarmerID));
                    sp.setDividerLocation(150);
                } else if (statisticManage.equals(lastPathComponent)){
                    //sp.setRightComponent(new JLabel("这里进行统计农产品的销售信息。..."));
                    sp.setRightComponent(new StatisticManageComponent(jf,FarmerID));
                    sp.setDividerLocation(150);
                }

            }
        });


        sp.setRightComponent(new ProductManageComponent(jf,FarmerID));
        sp.setLeftComponent(tree);
        jf.add(sp);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    //自定义结点绘制器
    private class MyRenderer extends DefaultTreeCellRenderer {
        private ImageIcon rootIcon = null;
        private ImageIcon productManageIcon = null;
        private ImageIcon orderManageIcon = null;
        private ImageIcon statisticManageIcon = null;

        public MyRenderer() {
            rootIcon = new ImageIcon(new GetImage().getImage("systemManage.png"));
            productManageIcon = new ImageIcon(new GetImage().getImage("Product.png"));
            orderManageIcon = new ImageIcon(new GetImage().getImage("Order.png"));
            statisticManageIcon = new ImageIcon(new GetImage().getImage("Statistic.png"));
        }

        //当绘制树的每个结点时，都会调用这个方法
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            //使用默认绘制
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

            ImageIcon image = null;
            switch (row) {
                case 0:
                    image = rootIcon;
                    break;
                case 1:
                    image = productManageIcon;
                    break;
                case 2:
                    image = orderManageIcon;
                    break;
                case 3:
                    image = statisticManageIcon;
                    break;
            }

            this.setIcon(image);
            return this;
        }
    }

//    public static void main(String[] args) {
//        try {
//            new FarmerInterface().init();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

}
